# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=mlir
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_version=15.0.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
pkgrel=1
pkgdesc="Multi-Level IR Compiler Framework for LLVM (mingw-w64)"
url="https://mlir.llvm.org/"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=("custom:Apache 2.0 with LLVM Exception")
depends=("${MINGW_PACKAGE_PREFIX}-llvm")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-clang"
)
options=('!debug' 'strip')
_pkgfn=$_realname
_url=https://github.com/llvm/llvm-project/releases/download/${_tag}
# No source package for MLIR: https://github.com/llvm/llvm-project/issues/51590
source=("${_url}/llvm-project-${pkgver}.src.tar.xz"{,.sig}
        0001-do-not-link-to-llvm-dylib.patch
        0002-Use-hidden-visibility-when-building-for-MinGW-with-Clang.patch)
sha256sums=('8b5fcb24b4128cf04df1b0b9410ce8b1a729cb3c544e6da885d234280dedeac6'
            'SKIP'
            '21e21fcd76e4dbb48032903b91ee7521051a91122c2a58616a33da02796b0580'
            'f3bce25ac7d339cbe7a94bd1cfd2d634450c261c7a32103aba4ed5f773fd2cfc')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard
              'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta
noextract=(llvm-project-${pkgver}.src.tar.xz)

prepare() {
  plain "Extracting llvm-project-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  rm -rf mlir || true
  tar --strip-components=1 -xJf ${srcdir}/llvm-project-${pkgver}.src.tar.xz -C ${srcdir} || true
  cd "${srcdir}"/mlir
  patch -p1 -i "${srcdir}"/0001-do-not-link-to-llvm-dylib.patch
  cd "${srcdir}"/llvm
  patch -p1 -i "${srcdir}"/0002-Use-hidden-visibility-when-building-for-MinGW-with-Clang.patch
}

build() {
  cd ${srcdir}
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  if check_option "debug" "y"; then
    _build_type="Debug"
  else
    _build_type="Release"
  fi

  CC=${MINGW_PREFIX}/bin/clang \
  CXX=${MINGW_PREFIX}/bin/clang++ \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DLLVM_HOST_TRIPLE="${CARCH}-w64-windows-gnu" \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DLLVM_BUILD_LLVM_DYLIB=ON \
    -DMLIR_LINK_MLIR_DYLIB=ON \
    -DLLVM_BUILD_TOOLS=ON \
    -DLLVM_BUILD_UTILS=ON \
    -DLLVM_ENABLE_PIC=ON \
    -DMLIR_INCLUDE_TESTS=OFF \
    -DCMAKE_C_VISIBILITY_PRESET=hidden \
    -DCMAKE_CXX_VISIBILITY_PRESET=hidden \
    -Wno-dev \
    ../$_pkgfn

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 ${srcdir}/${_realname}/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
}
